/*
 * hd44789-8w.c
 * Implements 8bit write operations
 *
 *  Created on: Sep 27, 2009
 *      Author: Orlando Arias
 *	   License: GPLv3
 *
 *   libHD44780 for Arduino systems
 *   Copyright (C) 2009  Orlando Arias
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "WProgram.h"

#include "hd44780-8.h"
#include "private-8.h"
#include "common/common_private.h"

void lcd8_init(LCD8 *l){
	// initialize data pins
	for (uint8_t i = 0; i<8; i++){
		pinMode(l->datapin[i], OUTPUT);
		digitalWrite(l->datapin[i], LOW);	// start with a clean low
	}

	// initialize strobe, read/write and register select pins
	pinMode(l->strobe, OUTPUT);
	digitalWrite(l->strobe, LOW);

	pinMode(l->rw_pin, OUTPUT);
	digitalWrite(l->rs_pin, LOW);

	pinMode(l->rs_pin, OUTPUT);
	digitalWrite(l->rs_pin, LOW);

	// begin LCD initialization, as per initialization in datasheet
	delay(50);					// delay at least 15ms
	lcd8_writeByte(l, 0x30);	// special case function
	lcd_triggerStrobe(l->strobe);
	delay(5);					// wait more than 4.1ms
	lcd8_writeByte(l, 0x30);
	lcd_triggerStrobe(l->strobe);
	delayMicroseconds(200);		// wait more than 100 us
	lcd8_writeByte(l, 0x30);
	lcd_triggerStrobe(l->strobe);
	delayMicroseconds(200);		// wait more than 100 us

	lcd8_sendCommand(l, 0x38);
	lcd8_sendCommand(l, BLANK_DISPLAY);
	lcd8_sendCommand(l, CLEAR);
    lcd8_sendCommand(l, EM_INCREMENT_OFF);
	lcd8_sendCommand(l, CURSOR_INVISIBLE);
}

void lcd8_clear(LCD8 *l){
	lcd8_sendCommand(l, CLEAR);
}

void lcd8_sendChar(LCD8 *l, uint8_t chr){
	lcd8_sendRaw(l, chr, HIGH);		// RS = 1, write to DDRAM
	while (lcd8_getState(l));		// Delay until busy flag is cleared
}

void lcd8_sendCommand(LCD8 *l, command cmd){
	lcd8_sendRaw(l, cmd, LOW);		// RS = 0, write command
	while (lcd8_getState(l));		// Delay until busy flag is cleared
}

void lcd8_sendStr(LCD8 *l, const char str[]){
    const char *p = str;
	while (*p)
      lcd8_sendChar(l, *(p++));
}

void lcd8_setAC(LCD8 *l, uint8_t row, uint8_t col){
	/*
	 *  Positions address counter in terms of row/column
	 *
	 *  NOTICE: Function assumes most standard layouts. Refer
	 *  to datasheet for details. If your LCD uses a different
	 *  screen layout, you may implement your own AC function
	 *  by using the call:
	 *  lcd8_sendCommand(l, SET_POSITION | offset);
	 */
	uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
	lcd8_sendCommand(l, SET_POSITION | (col + row_offsets[row]));
}

void lcd8_writeCGRAM(LCD8 *l, uint8_t location, uint8_t data[]){
	/*
	 *  Consult the documentation on how to construct the
	 *  data[] array
	 */
	if (location > 7) return;		// if invalid location, exit function
	uint8_t AC = lcd8_getAC(l);		// get AC
	lcd8_sendCommand(l, 0x40 | (location<<3));		// set CGRAM location
	digitalWrite(l->rs_pin, HIGH);			// set up write operation
	// write and commit data
	for (uint8_t i = 0; i<8; i++){
		lcd8_writeByte(l, data[i]);
		lcd_triggerStrobe(l->strobe);
	}
	// restore AC position
	lcd8_sendCommand(l, SET_POSITION | AC);
}
